package org.litemall.netty.controller;

import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.litemall.netty.exection.ServiceExection;
import org.litemall.netty.nettyDoman.Result;
import org.litemall.netty.nettyDoman.SocketMessage;
import org.litemall.netty.nettyDoman.sendUserMessageVO;
import org.litemall.netty.service.ChannelSupervise;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @Author Lizr
 * @Description socket消息控制层
 * @CreateTime 2021-12-17 下午 2:38
 **/
@RestController
public class SocketController {
    /**
     * 向用户发送socket消息
     * @param user{
     *              id: 用户ID
     *              content: 内容
     *              type : 内容类型, 1 text格式消息
     *            }
     * @return
     */
    @RequestMapping("/sendUserMessage")
    public Result sendUserMessage(@RequestBody @Valid sendUserMessageVO user){
        switch (user.getType()){
            case 1:
                String body = SocketMessage.sendUserNotice(user.getContent()).toString();
                TextWebSocketFrame textWebSocketFrame = new TextWebSocketFrame(body);
                ChannelSupervise.sendUserMessageById(user.getId(),textWebSocketFrame);
                break;
            default:
                new ServiceExection("不支持的消息类型");
                break;
        }
        return Result.success(null);
    }

}
